using I2.Loc;
using UnityEngine;

public class TitanSpawner : MonoBehaviour
{
	private static TitanSpawner _instance;

	public float maxSpawnRechargeValue = 100f;

	public float _titanSpawnRecharge;

	public ParticleSystem spawnTitanParticles;

	public float killPoints = 15f;

	public float timePoints = 10f;

	public float damagePointsMuliplaer = 1f;

	public float myDamagePointsMuliplaer = 0.25f;

	private bool _canSpawnTitanHere;

	public LayerMask layerMask;

	public BoxCollider trigger;

	public Vector3 halfExtents = new Vector3(5f, 25f, 4f);

	public float additinalForwardOfset = 0.5f;

	public float additinalUpOfset = 0.5f;

	private Collider[] collidersOnWay;

	public TitanBehavior currentTitan;

	public static TitanSpawner instance
	{
		get
		{
			if (_instance == null)
			{
				_instance = ((GameObject)Object.Instantiate(Resources.Load("TitanSpawner"))).GetComponent<TitanSpawner>();
			}
			return _instance;
		}
	}

	public float titanSpawnRecharge
	{
		get
		{
			return _titanSpawnRecharge;
		}
		set
		{
			if (!(GameController.instance == null) && GameController.instance.curTypeGame == TypeGame.TitanfallV2 && PVPManager.instance.gameIsStarted && !(PlayerBehavior.MyPlayer == null) && (!(PlayerBehavior.MyPlayer.currentCar != null) || !(PlayerBehavior.MyPlayer.currentCar is TitanBehavior)))
			{
				if (_titanSpawnRecharge < maxSpawnRechargeValue && value >= maxSpawnRechargeValue)
				{
					GameplayNotificationController.instance.AddNotification(ScriptLocalization.Get("Game_Common/Titan_youCanSummon"), 0);
					TitanTutor.instance.ShowArrowByFullBar();
				}
				_titanSpawnRecharge = value;
			}
		}
	}

	public bool canSpawnTitan
	{
		get
		{
			return titanSpawnRecharge >= maxSpawnRechargeValue;
		}
	}

	public bool canSpawnTitanHere
	{
		get
		{
			if (!canSpawnTitan || PlayerBehavior.MyPlayer.inCar || PlayerBehavior.MyPlayer.isDead || PlayerBehavior.MyPlayer.isShock || !PlayerBehavior.MyPlayer.canMove)
			{
				return false;
			}
			return _canSpawnTitanHere;
		}
	}

	public PositionCar titanSpawnPosition
	{
		get
		{
			PositionCar result = default(PositionCar);
			result.pos = Vector3.zero;
			result.rot = Quaternion.identity;
			SetSpawnerPosition();
			if (CheckTitanSpawnAbility())
			{
				result.pos = base.transform.position + base.transform.forward * halfExtents.x * 0.5f;
				result.rot = base.transform.rotation;
			}
			return result;
		}
	}

	public void ResetTitanSpawnRecharge()
	{
		_titanSpawnRecharge = 0f;
	}

	private void Start()
	{
		switch (controllerConnectPhoton.thisScript.playerBoiler)
		{
		case 0:
			maxSpawnRechargeValue *= 1f;
			timePoints *= 1f;
			killPoints *= 1f;
			break;
		case 1:
			maxSpawnRechargeValue *= 2f;
			timePoints *= 2f;
			killPoints *= 2f;
			break;
		case 2:
			maxSpawnRechargeValue *= 4f;
			timePoints *= 4f;
			killPoints *= 4f;
			break;
		default:
			maxSpawnRechargeValue *= 1f;
			timePoints *= 1f;
			killPoints *= 1f;
			break;
		}
		if (GameController.instance.curTypeGame == TypeGame.WalkingRobots)
		{
			halfExtents.Set(halfExtents.x, 10f, halfExtents.z);
			additinalForwardOfset = 0f - halfExtents.x;
		}
	}

	public void HideMyTitanWithDelay(float delay)
	{
		if (currentTitan != null)
		{
			currentTitan.PropperlyHideTitan(delay);
		}
	}

	private void Update()
	{
		if (!canSpawnTitan)
		{
			titanSpawnRecharge += timePoints * Time.unscaledDeltaTime;
			return;
		}
		SetSpawnerPosition();
		_canSpawnTitanHere = CheckTitanSpawnAbility();
	}

	private void SetSpawnerPosition()
	{
		if (!(PlayerBehavior.MyPlayer == null))
		{
			Vector3 vector = new Vector3(TPSCamera.instance.transform.forward.x, 0f, TPSCamera.instance.transform.forward.z);
			Vector3 normalized = vector.normalized;
			base.transform.position = PlayerBehavior.MyPlayer.transform.position + normalized * (halfExtents.z + additinalForwardOfset) + Vector3.up * (halfExtents.y + additinalUpOfset);
			base.transform.rotation = Quaternion.Euler(0f, TPSCamera.instance.transform.rotation.eulerAngles.y, 0f);
		}
	}

	private bool CheckTitanSpawnAbility()
	{
		trigger.size = halfExtents * 2f;
		return Physics.OverlapBox(base.transform.position, halfExtents, base.transform.rotation, layerMask).Length == 0;
	}

	public void SpawnTitan()
	{
		RaycastHit hitInfo;
		if (canSpawnTitan && canSpawnTitanHere && PlayerBehavior.MyPlayer.isGrounded && (!Physics.Raycast(base.transform.position, Vector3.down, out hitInfo, 150f, layerMask) || (!(hitInfo.collider.gameObject.tag == "Water") && !(hitInfo.point.y < PlayerBehavior.MyPlayer.transform.position.y - 4f))))
		{
			if (currentTitan != null && currentTitan.name != settings.SelectedTitanName)
			{
				PhotonNetwork.Destroy(currentTitan.gameObject);
				currentTitan = null;
			}
			if (currentTitan != null && currentTitan.name == settings.SelectedTitanName)
			{
				currentTitan.photonView.RPC("reset", PhotonTargets.Others);
				currentTitan.reset();
			}
			else
			{
				currentTitan = PhotonNetwork.Instantiate("Cars/PrivateCars/" + settings.SelectedTitanName, Vector3.down * 1000f, Quaternion.identity, 0).GetComponent<TitanBehavior>();
				currentTitan.name = settings.SelectedTitanName;
			}
			PositionCar positionCar = titanSpawnPosition;
			currentTitan.transform.position = positionCar.pos;
			currentTitan.transform.rotation = positionCar.rot;
			currentTitan.SpawnTitanAnimations();
		}
	}
}
